﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YRevitJig;

namespace HYFloorPipeModule
{
	public class FloorPipeDynamicDrag : XUiJigBase
	{
		public FloorPipeDynamicDrag(UIDocument uiDocument) : base(uiDocument)
		{
			this.view = uiDocument.Document.ActiveView;
		}

		public XYZ StartPoint { get; set; }

		public QuadrantLocation PointQuadrant
		{
			get
			{
				return this.pointQuadrant;
			}
		}

		public XYZ MarkPoint
		{
			get
			{
				return this.markPoint;
			}
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			try
			{
				if (!this.m_ptCur.IsEmpty && paintEventArgs != null)
				{
					XYZ xyz = base.Client2Revit(this.m_ptCur);
					if (xyz != null)
					{
						XYZ screenStartPnt = new XYZ(xyz.X, xyz.Y, this.StartPoint.Z);
						List<Line> drawLines = this.GetDrawLines(this.view, screenStartPnt);
						XGraphics g = new XGraphics(paintEventArgs.Graphics, base.JigPen);
						foreach (Line curve in drawLines)
						{
							new XLine2D(base.Revit2Client(curve.GetEndPoint(0)), base.Revit2Client(curve.GetEndPoint(1))).Draw(g);
						}
					}
				}
			}
			catch (Exception)
			{
			}
		}

		private List<Line> GetDrawLines(Autodesk.Revit.DB.View view, XYZ screenStartPnt)
		{
			List<Line> list = new List<Line>();
			YJKLine hyline = new YJKLine(this.StartPoint, screenStartPnt);
			list.Add(hyline.Line);
			List<XYZ> rectanglePoints = this.GetRectanglePoints(this.StartPoint, screenStartPnt, view);
			if (rectanglePoints == null || rectanglePoints.Count != 4)
			{
				return list;
			}
			this.markPoint = rectanglePoints[0].Add(rectanglePoints[1]) / 2.0;
			hyline = new YJKLine(rectanglePoints[0], rectanglePoints[1]);
			list.Add(hyline.Line);
			hyline = new YJKLine(rectanglePoints[1], rectanglePoints[2]);
			list.Add(hyline.Line);
			hyline = new YJKLine(rectanglePoints[2], rectanglePoints[3]);
			list.Add(hyline.Line);
			hyline = new YJKLine(rectanglePoints[3], rectanglePoints[0]);
			list.Add(hyline.Line);
			return list;
		}

		private List<XYZ> GetRectanglePoints(XYZ startPoint, XYZ screenStartPnt, Autodesk.Revit.DB.View view)
		{
			double num = UnitConvert.CovertToAPI(700.0, (Autodesk.Revit.DB.DisplayUnitType)2) * (double)view.Scale / 100.0;
			double num2 = 2.5 * num;
			XYZ xyz = null;
			XYZ xyz2 = null;
			XYZ item = null;
			XYZ item2 = null;
			double num3 = 0.1;
			if (startPoint.X < screenStartPnt.X && Math.Abs(startPoint.Y - screenStartPnt.Y) < num3)
			{
				this.pointQuadrant = QuadrantLocation.XPosition;
				xyz2 = screenStartPnt.Add(num / 2.0 * view.UpDirection);
				xyz = screenStartPnt.Subtract(num / 2.0 * view.UpDirection);
				item = xyz.Add(num2 * view.RightDirection);
				item2 = xyz2.Add(num2 * view.RightDirection);
			}
			else if (Math.Abs(startPoint.X - screenStartPnt.X) < num3 && startPoint.Y > screenStartPnt.Y)
			{
				this.pointQuadrant = QuadrantLocation.YNegative;
				xyz2 = screenStartPnt.Subtract(num2 / 2.0 * view.RightDirection);
				xyz = xyz2.Subtract(num * view.UpDirection);
				item = xyz.Add(num2 * view.RightDirection);
				item2 = xyz2.Add(num2 * view.RightDirection);
			}
			else if (startPoint.X > screenStartPnt.X && Math.Abs(startPoint.Y - screenStartPnt.Y) < num3)
			{
				this.pointQuadrant = QuadrantLocation.XNegative;
				xyz2 = screenStartPnt.Subtract(num2 * view.RightDirection);
				xyz2 = xyz2.Add(num / 2.0 * view.UpDirection);
				xyz = xyz2.Subtract(num * view.UpDirection);
				item = xyz.Add(num2 * view.RightDirection);
				item2 = xyz2.Add(num2 * view.RightDirection);
			}
			else if (Math.Abs(startPoint.X - screenStartPnt.X) < num3 && startPoint.Y < screenStartPnt.Y)
			{
				this.pointQuadrant = QuadrantLocation.YPosition;
				xyz2 = screenStartPnt.Subtract(num2 / 2.0 * view.RightDirection);
				xyz2 = xyz2.Add(num * view.UpDirection);
				xyz = xyz2.Subtract(num * view.UpDirection);
				item = xyz.Add(num2 * view.RightDirection);
				item2 = xyz2.Add(num2 * view.RightDirection);
			}
			else if (startPoint.X < screenStartPnt.X && startPoint.Y < screenStartPnt.Y)
			{
				this.pointQuadrant = QuadrantLocation.First;
				xyz2 = screenStartPnt.Add(num * view.UpDirection);
				xyz = screenStartPnt;
				item = xyz.Add(num2 * view.RightDirection);
				item2 = xyz2.Add(num2 * view.RightDirection);
			}
			else if (startPoint.X < screenStartPnt.X && startPoint.Y > screenStartPnt.Y)
			{
				this.pointQuadrant = QuadrantLocation.Second;
				xyz2 = screenStartPnt;
				xyz = screenStartPnt.Subtract(num * view.UpDirection);
				item = xyz.Add(num2 * view.RightDirection);
				item2 = xyz2.Add(num2 * view.RightDirection);
			}
			else if (startPoint.X > screenStartPnt.X && startPoint.Y > screenStartPnt.Y)
			{
				this.pointQuadrant = QuadrantLocation.Third;
				xyz2 = screenStartPnt.Subtract(num2 * view.RightDirection);
				xyz = xyz2.Subtract(num * view.UpDirection);
				item = xyz.Add(num2 * view.RightDirection);
				item2 = xyz2.Add(num2 * view.RightDirection);
			}
			else if (startPoint.X > screenStartPnt.X && startPoint.Y < screenStartPnt.Y)
			{
				this.pointQuadrant = QuadrantLocation.Fourth;
				xyz2 = screenStartPnt.Subtract(num2 * view.RightDirection);
				xyz2 = xyz2.Add(num * view.UpDirection);
				xyz = xyz2.Subtract(num * view.UpDirection);
				item = xyz.Add(num2 * view.RightDirection);
				item2 = xyz2.Add(num2 * view.RightDirection);
			}
			return new List<XYZ>
			{
				xyz2,
				xyz,
				item,
				item2
			};
		}

		private Autodesk.Revit.DB.View view;

		private QuadrantLocation pointQuadrant;

		private XYZ markPoint;
	}
}
